package com.corticon.dialogs.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;


/**
 * 
 * @author Lifan Yang
 */
@NamedQueries({@NamedQuery(name = "QuestionSetInNaire.findByQuestionnaireAndSequence", query =
"from QuestionSetInNaire qsin where questionnaire = :qn and sequence = :seq")})
@Entity
@Table(name = "q_dialog_page", uniqueConstraints = {@UniqueConstraint(columnNames = {
    "dialogassoc_q_dialog_id_oid", "pageassoc_q_page_id_oid"})})
public class QuestionSetInNaire extends AuditableEntity implements Serializable, Comparable {

//    private QuestionSetInNaireId id = new QuestionSetInNaireId();
private Long id;
private Questionnaire questionnaire;
private QuestionSet questionSet;
private String sequence;

public QuestionSetInNaire() {
}

public QuestionSetInNaire(Questionnaire questionnaire, QuestionSet questionSet, String sequence) {
    this.questionnaire = questionnaire;
    this.questionSet = questionSet;
    this.sequence = sequence;
}

@Id
@SequenceGenerator(name = "q_dialog_page_id_seq",
                   sequenceName = "q_dialog_page_id_seq",
                   allocationSize = 1, initialValue = 1) // Set allocationSize and initialValue
                                                         // to let sequence increment following DB.
@GeneratedValue(strategy = GenerationType.AUTO, generator =
"q_dialog_page_id_seq")
@Column(name = "q_dialog_page_id",  nullable = false, columnDefinition = "integer")
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "dialogassoc_q_dialog_id_oid")
public Questionnaire getQuestionnaire() {
    return this.questionnaire;
}

public void setQuestionnaire(Questionnaire questionnaire) {
    this.questionnaire = questionnaire;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "pageassoc_q_page_id_oid")
public QuestionSet getQuestionSet() {
    return this.questionSet;
}

public void setQuestionSet(QuestionSet questionSet) {
    this.questionSet = questionSet;
}

/**
 * SEQUENCE is reserved keyword of SQL-99.
 */
//    @Column(name = "_SEQUENCE")
@Column(name = "sequence")
public String getSequence() {
    return this.sequence;
}

public void setSequence(String sequence) {
    this.sequence = sequence;
}

// ********** Helper Methods ********** //
public void remove() {
    this.questionSet.getQuesitonSetInNaires().remove(this);
    this.questionnaire.getQuestionSetsInNaire().remove(this);
    }
    
    public int compareTo(Object o){
        return this.compareSequence(this.getSequence(), ((QuestionSetInNaire)o).getSequence());
    }

    public static int compareSequence(String small, String big){
        int is = Integer.parseInt(small);
        int ib = Integer.parseInt(big);
        int result = 0;
        if (is != ib){
            result = is - ib;
        }
        return result;
    }
}